Imports RIBS.BusinessLogicLayer.Reports
Imports RIBS.Code.GlobalModule1
Public Class frmStatus

    Public Function BindReport(ByVal ds As DataSet) As Boolean
        Try
            Cursor = Cursors.WaitCursor
            CrystalReportViewer1.Enabled = True
            CrystalReportViewer1.Visible = True
            Application.DoEvents()
            Dim oRpt As ReportDocument

            oRpt = CrystalReportViewer1.ReportSource
            oRpt.SetDataSource(ds)

            CrystalReportViewer1.ReportSource = oRpt
            CrystalReportViewer1.Zoom(150)
            Cursor = Cursors.Default
        Catch ex As Exception
            DisplayErrors(ex)
        End Try

    End Function


    Private Sub btnRefresh_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Try
            Dim ds As New DataSet
            Dim UserList As String = ""
            Dim Actions As String = ""
            Cursor = Cursors.WaitCursor
            Application.DoEvents()
            '*********************************************************
            'Options
            ds.Tables.Add(New dsDBSchema.OptionsDataTable)

            Dim dr As DataRow
            dr = ds.Tables("Options").NewRow
            dr("FromDate") = dtpickfromDate.Text
            dr("ToDate") = dtpickToDate.Text
            dr("ReportTitle") = txtReportTitle.Text
            dr("PageTitle") = txtPageTitle.Text
            ds.Tables("Options").Rows.Add(dr)
            '*********************************************************
            ' Users
            ds.Tables.Add("Users")
            ds.Tables("Users").Columns.Add("User", GetType(String))

            For Each itm As Object In chkListUsers.CheckedIndices
                UserList += chkListUsers.Items(itm) & ","
                Dim drUser As DataRow
                drUser = ds.Tables("Users").NewRow
                drUser("User") = chkListUsers.Items(itm)
                ds.Tables("Users").Rows.Add(drUser)
            Next
            '*********************************************************
            ' Action
            ds.Tables.Add("Action")
            ds.Tables("Action").Columns.Add("Action", GetType(String))

            For Each itm As Object In chkListAction.CheckedIndices
                Actions += chkListAction.Items(itm) & ","
                Dim drAction As DataRow
                drAction = ds.Tables("Action").NewRow
                drAction("Action") = chkListAction.Items(itm)
                ds.Tables("Action").Rows.Add(drAction)
            Next
            My.Settings.ReportsActions = Actions
            My.Settings.ReportsUserList = UserList
            My.Settings.ReportsStatusReportTitle = txtReportTitle.Text
            My.Settings.ReportsStatusPageTitle = txtPageTitle.Text
            My.Settings.Save()

            
            BindReport(StatusReport.StatusReport(ds))
            Cursor = Cursors.Default
        Catch ex As Exception
            DisplayErrors(ex)
        End Try

    End Sub

    Private Sub frmStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Cursor = Cursors.WaitCursor
            Dim oRibs As New RibsClass
            Dim dt As DataTable
            Dim UserList As String = My.Settings.ReportsUserList
            Dim Actions As String = My.Settings.ReportsActions
            txtReportTitle.Text = My.Settings.ReportsStatusReportTitle
            txtPageTitle.Text = My.Settings.ReportsStatusPageTitle

            dtpickfromDate.Text = DateAdd(DateInterval.Day, -10, Now())
            dtpickToDate.Text = Now()
            CrystalReportViewer1.Enabled = False
            CrystalReportViewer1.Visible = False
            'CrystalReportViewer1.ReportSource = Nothing
            dt = oRibs.GetActions(False)
            chkListAction.Items.Clear()

            For Each dr As DataRow In dt.Rows
                If dr(0).ToString <> RIBSState.Unallocated.ToString Then
                    If InStr(Actions, dr(0) & ",") > 0 Then
                        chkListAction.Items.Add(dr(0), True)
                    Else
                        chkListAction.Items.Add(dr(0), False)
                    End If
                End If
            Next

            Dim oUsers As New dbUser
            Dim dtusrs As DataTable = oUsers.GetAllUsers
            chkListUsers.Items.Clear()

            For Each dr As DataRow In dtusrs.Rows
                If dr(0).ToString <> RIBSState.Unallocated.ToString Then
                    If InStr(UserList.ToString.ToUpper, dr(0).ToString.ToUpper & ",") > 0 Then
                        chkListUsers.Items.Add(dr(0).ToString.ToUpper, True)
                    Else
                        chkListUsers.Items.Add(dr(0).ToString.ToUpper, False)
                    End If
                End If
            Next

        Catch ex As Exception
            DisplayErrors(ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

End Class